package com.gm.controller.customer;

import com.github.miemiedev.mybatis.paginator.domain.PageList;
import com.github.miemiedev.mybatis.paginator.domain.Paginator;
import com.gm.common.Pager;
import com.gm.controller.base.RspVo;
import com.gm.model.Base.Const;
import com.gm.model.po.CardType;
import com.gm.model.po.Customer;
import com.gm.model.po.Rate;
import com.gm.model.vo.ValidVo;
import com.gm.service.user.CustomerService;
import com.gm.service.user.RateService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;

/**
 * Created by taojie on 17-6-21.
 */
@Controller
@RequestMapping("customer")
public class CustomerController {

    Logger logger = LoggerFactory.getLogger(CustomerController.class);

    @Autowired
    private CustomerService customerService;
    @Autowired
    private RateService rateService;


    @RequestMapping("customerList.web")
    public ModelAndView customerList(Pager pager){
        logger.info("Method[customerList],getCurrentPage[{}]",pager.getCurrentPage());
        ModelAndView view= new ModelAndView();
        PageList<Customer> customerList = customerService.getCustomerList(pager);
        logger.info("Method[customerList],customerList.size[{}]",customerList == null?null:customerList.size());
        List<Rate> rateList = rateService.getRateAll();
        logger.info("Method[customerList],rateList.size[{}]", rateList == null?null:rateList.size());
        Paginator paginator =customerList.getPaginator();
        view.addObject("customerList", customerList);
        view.addObject("rateList", rateList);
        view.addObject("paginator", paginator);
        view.setViewName("/customer/customer.jsp");
        return view;
    }

    @RequestMapping("gotoAddCustomer.web")
    public ModelAndView gotoAddCustomer(){
        ModelAndView view= new ModelAndView();
        List<Rate> rateList = rateService.getRateAll();
        view.addObject("rateList", rateList);
        view.setViewName("/customer/addCustomer.jsp");
        return view;
    }

    @RequestMapping(value = "checkCustomerCodeOnly.do",method = RequestMethod.POST)
    @ResponseBody
    public ValidVo checkCustomerCodeOnly(@RequestParam(value = "code", required = true) String code, RedirectAttributes attr){
        logger.info("Method[checkCustomerCodeOnly],code[{}]",code);
        ValidVo validVo = new ValidVo();
        List customerList = customerService.selectByCode(code);
        if(customerList == null || customerList.size() == 0){
            validVo.setValid(true);
        }else{
            validVo.setValid(false);
        }
        logger.info("Method[checkCustomerCodeOnly],validVo.isValid[{}]",validVo.isValid());
        return validVo;
    }

    @RequestMapping(value = "checkCustomerIdCardOnly.do",method = RequestMethod.POST)
    @ResponseBody
    public ValidVo checkCustomerIdCardOnly(@RequestParam(value = "idCard", required = true) String idCard, RedirectAttributes attr){
        logger.info("Method[checkCustomerIdCardOnly],idCard[{}]",idCard);
        ValidVo validVo = new ValidVo();
        List customerList = customerService.selectByIdCard(idCard);
        if(customerList == null || customerList.size() == 0){
            validVo.setValid(true);
        }else{
            validVo.setValid(false);
        }
        logger.info("Method[checkCustomerIdCardOnly],validVo.isValid[{}]",validVo.isValid());
        return validVo;
    }

    @RequestMapping(value = "addCustomer.do",method = RequestMethod.POST)
    @ResponseBody
    public RspVo addCustomer(Customer customer, Integer rateId, RedirectAttributes attr){
        logger.info("Method[addCustomer],customer[{}],rateId[{}]",customer.toString(),rateId);
        RspVo rspVo = new RspVo();
        customer.setRid(rateId);
        int ret = customerService.addCustomer(customer);
        if(ret == 1){
            rspVo.setStatus(Const.RES_OK);
        }else{
            rspVo.setError("客户信息添加失败");
        }
        logger.info("Method[addCustomer], status[{}]", rspVo.getStatus());
        return rspVo;
    }

    @RequestMapping("gotoUpdateCustomer.web")
    public ModelAndView gotoUpdateUser(Integer customerId,Integer currentPage){
        ModelAndView view= new ModelAndView();
        Customer customer = customerService.selectByPrimaryKey(customerId);
        List<Rate> rateList = rateService.getRateAll();
        view.addObject("rateList", rateList);
        view.addObject("currentPage", currentPage);
        view.addObject("customer", customer);
        view.setViewName("/customer/updateCustomer.jsp");
        return view;
    }

    @RequestMapping(value = "updateCustomer.do", method = RequestMethod.POST)
    @ResponseBody
    public RspVo updateCustomer(Customer customer,Integer rateId, Integer currentPage,RedirectAttributes attr){
        logger.info("Method[updateCustomer],customer[{}],rateId[{}]",customer.toString(),rateId);
        RspVo rspVo = new RspVo();
        customer.setRid(rateId);
        int ret = customerService.updateCustomer(customer);
        if(ret == 1){
            rspVo.setStatus(Const.RES_OK);
        }else{
            rspVo.setError("客户信息修改失败");
        }
        logger.info("Method[updateCustomer], status[{}]", rspVo.getStatus());

        return rspVo;
    }


    @RequestMapping("deleteCustomer.web")
    public ModelAndView deleteUser(Integer customerId,Integer currentPage,RedirectAttributes attr){
        ModelAndView view= new ModelAndView();
        int ret = customerService.deleteCustomer(customerId);
        if(ret==1){
            attr.addFlashAttribute("msg", "删除成功");
        }else{
            attr.addFlashAttribute("msg", "删除失败");
        }
        Pager pager = new Pager();
        pager.setCurrentPage(currentPage);
        List userList = customerService.getCustomerList(pager);
        if(userList.size() == 0){
            currentPage = currentPage -1;
        }
        if(currentPage==0)currentPage=1;
        view.addObject("currentPage", currentPage);
        view.setViewName("redirect:/customer/customerList.web");
        return view;
    }
}
